/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui.core;

import java.util.ArrayList;
import java.util.HashMap;

import android.view.View;
import android.view.ViewGroup;

import com.mol.market.demo.widget.LazyLoadAbsListViewFragment;

/**
 * 
 * @author Chaos	
 * @date 2013-2-6 
 */
public class LazyLoadAdapter extends CommonAdapter{
    
    private LazyLoadAbsListViewFragment mLazyLoadFragment;

    private boolean isLazyLoad = true;
    
    public LazyLoadAdapter(LazyLoadAbsListViewFragment fragment, ArrayList<HashMap<String, Object>> data, int resource,
            String[] from, int[] to) {
        super(fragment.getActivity(), data, resource, from, to);
        
        mLazyLoadFragment = fragment;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if(isLazyLoad && position == getCount() - 4){
            doLazyLoad();
        }
        
        return super.getView(position, convertView, parent);
    }
    
    protected void doLazyLoad() {
        mLazyLoadFragment.doLazyLoad();
    }

    public boolean isLazyLoad() {
        return isLazyLoad;
    }

    public void closeLazyLoad(){
        this.isLazyLoad = false;
    }
    
    public void openLazyLoad(){
        this.isLazyLoad = true;
    }
    
    /**
     * Lazyload linstener If you want use the lazyload function, must implements
     * this interface
     */
    public interface LazyloadListener {

        /**
         * You should implements this method to justify whether should do
         * lazyload
         *
         * @return
         */
        boolean isEnd();

        /**
         * Do something that process lazyload
         */
        void lazyload();

        /**
         * Indicate whether the loading process is over
         *
         * @return
         */
        boolean isLoadOver();
    }

}
